#  coding=utf8
import time

from loguru import logger
import uiautomator2 as u2


class ZhiHu(object):
    def __init__(self, in_user="15231109698", in_pwd="qq901212323"):

        self.d = u2.connect('103.36.203.71:30001')  # connect to device

        self.app_package_name = "com.zhihu.android"
        self.user_name = in_user
        self.pwd = in_pwd
        logger.info("账号为 {}， 密码为 {}".format(self.user_name, self.pwd))
        logger.info("正在初始化app中。。。。")
        self.d.press("home")
        self.d.app_info(self.app_package_name)
        self.d.app_clear(self.app_package_name)
        logger.info("Get WLAN ip {}".format(self.d.wlan_ip))
        self.d.healthcheck()

    def start_app(self):
        logger.info("启动app")
        self.d(resourceId="com.chinac.launcher:id/tv_appName", text="知乎").click()
        pid = self.d.app_wait(self.app_package_name)  # 等待应用运行, return pid(int)
        if not pid:
            raise Exception("{} is not running".format("self.app_package_name"))

    def inter_app(self):
        logger.info("进入app中，同意用户协议")

        self.d(resourceId="android:id/button1").click()

    def ying_yong_nei_ce(self):
        """
        取消应用市场内测
        :return:
        """
        if self.d.xpath('//*[text="内测"]').exists:
            self.d.xpath('//*[text="取消"]').click()

    def login_app(self):
        logger.info("进行手机号登录")
        self.d(resourceId="com.zhihu.android:id/tv_password").click()
        self.d(resourceId="com.zhihu.android:id/email_input_view").click()
        self.d.send_keys(self.user_name, clear=True)
        self.d(resourceId="com.zhihu.android:id/password").click()
        self.d.send_keys(self.pwd, clear=True)
        self.d(resourceId="com.zhihu.android:id/btn_func").click()
        logger.success("登录成功")

    def smtp_val(self):
        # self.d(resourceId="com.miui.home:id/icon_icon", description="知乎").click()
        self.d.app_start(self.app_package_name, stop=True)
        time.sleep(20)
        self.d.xpath('重设密码').click()
        self.d.watcher.when("重设密码").click()
        self.d.watcher.when("取消").click()
        # self.d.xpath('重设密码').click()
        # if self.d.xpath('重设密码').exists:
        #     self.d.xpath('重设密码').click()
        #     self.d.xpath("发送验证码").click()
        #     self.d(resourceId="com.zhihu.android:id/text_code").click()
        #     self.d.send_keys(input("请输入邮箱验证码"))

    def check_ui(self):
        try:
            self.d(resourceId="com.android.packageinstaller:id/permission_allow_button").click()
            self.d(resourceId="com.android.packageinstaller:id/permission_allow_button").click()
            self.d(resourceId="com.android.packageinstaller:id/permission_allow_button").click()
            self.d(resourceId="com.android.packageinstaller:id/permission_allow_button").click()
        except:
            print("验证通过。。")

    def search_app(self, kwd="2年python编程自学经历"):
        logger.info("正在查询关键词")
        self.d(resourceId="com.zhihu.android:id/input").click()
        self.d.send_keys(kwd, clear=True)
        logger.info("正在滑动屏幕，寻找特定答案")
        while 1:
            self.d.swipe_ext("up", scale=0.8)
            if self.d.xpath('//*[@text="2年python编程自学经历，分享一些学习心得和避坑经验"]').exists:
                self.d.xpath('//*[@text="2年python编程自学经历，分享一些学习心得和避坑经验"]').click()
                logger.success("2年python编程自学经历，分享一些学习心得和避坑经验")
                for i in range(20):
                    logger.info("正在浏览、、、")
                    self.d.swipe_ext("up", scale=0.6)
                    if self.d.xpath('//*[@text="上拉查看"]').exists or self.d.xpath('//*[@text="松开查看"]').exists:
                        break
                logger.info("浏览完成、、、 结束当前账号进程")
                break

    def run(self):
        self.start_app()
        # self.inter_app()
        self.login_app()
        # self.smtp_val()
        self.check_ui()
        self.search_app()


# d.app_start(app_package_name, stop=True) # 启动应用前停止应用
# app = d.session(app_package_name) # 启动应用并获取session
if __name__ == '__main__':
    user = "17083040107@163.com"
    pwd = "bai111111"
    a = ZhiHu()
    a.run()

# See PyCharm help at https://www.jetbrains.com/help/pycharm/
